<?php

class HomeController extends Controller
{
	/*public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}*/
	public function allowedActions()
	{
		return '*';
	}

	public function actionIndex()
	{
		
		$this->pageTitle = $this->arrSystem['title'];
		$criteriaNew = new CDbCriteria;
		$criteriaNew->limit = 4;
		$criteriaNew->order = "id DESC";
		$arrNews =  News::model()->findAll($criteriaNew);
		
		$arrInformation = Informations::model()->findAll();
		
		$criteriaLink = new CDbCriteria;  
		$criteriaLink->addCondition("is_support=0");
		$arrLink = Links::model()->findAll($criteriaLink);
		
		
		$this->render("index", array('arrNews'=>$arrNews, 'arrInformation'=>$arrInformation, 'arrLink'=>$arrLink));
	}
	
	public function actionInformation(){
	
		$model = Informations::model()->findByPk(1);
		$this->render("information", array("model" => $model));
	}
	
	public function actionProcedure(){
	
		$criteriaInfo = new CDbCriteria;  
		$criteriaInfo->addCondition("type_document=3");		

		$arrInfo = Documents::model()->findAll($criteriaInfo);
		
		$this->render("procedure", array(
			"arrInfo" => $arrInfo,
		));

	}
	
	public function actionDownload($name = null){
	
		$arrInfo = Documents::model()->findByPk($name);
		
		$pathUrl = Yii::app()->request->hostInfo . Yii::app()->request->baseURL . '/upload/documents/' .$arrInfo->md5name;
		$path = Yii::app()->basePath.'/../upload/documents/' .$arrInfo->md5name;

		if(file_exists($path))
		{
			return Yii::app()->getRequest()->sendFile(alias($arrInfo->name).".".$arrInfo->type, @file_get_contents($pathUrl));
		}

		$this->redirect(PIUrl::createUrl('/home/procedure'));
	}
	
	public function actionFee(){
		
		$criteriaInfo = new CDbCriteria;  
		$criteriaInfo->addCondition("id=2");
		$arrInfo = Informations::model()->findAll($criteriaInfo);
		
		$this->render("fee", array("arrInfo" => $arrInfo));
	}
	
	public function actionDocument(){
		$arrInfo = CategoriesDocuments::model()->findAll();
		$criteria = new CDbCriteria;  
		$criteria->addCondition("type_document=1");	
		$arrDocument = Documents::model()->findAll($criteria);
		
		$this->render("document", array(
			"arrDocument"=>$arrDocument, 
			"arrCategory"=>$arrInfo,
		));
	}
	
	public function actionDocumentLaw(){ 	
		
		$criteriaInfo = new CDbCriteria;  
		$criteriaInfo->addCondition("type_document=2");		

		$arrInfo = Documents::model()->findAll($criteriaInfo);
		
		$this->render("documentLaw", array(
			"arrInfo" => $arrInfo,
		));
	}
	
	
	public function actionContact(){
		if(!empty($_POST)){
			$content =  "Họ tên: ".$_POST['name']."\n";
			$content .=  "Email: ".$_POST['email']."\n";
			$content .=  "Tiêu đề: ".$_POST['title']."\n";
			$content .=  "Số điện thoại: ".$_POST['phone']."\n";
			$content .=  "Nội dung: ".$_POST['content']."\n";
			
			jsonOut(array('error'=>false,'msg'=>translate('Liên hệ thành công.')), false);
			
			$this->send_Mail($_POST['title'], $content);
			
		}else	$this->render("contactComment");
	}
	
	public function actionSchedule(){
		$model = new CommentsSchedules;
		if(isset($_POST['CommentsSchedules'])){
			$model->attributes = $_POST['CommentsSchedules'];
			$model->created = time();
			$model->is_schedule = 1;

			if($model->save())
			{
				Yii::app()->user->setFlash('success', translate('Đặt hẹn trước thành công.'));
			}
		}
		$this->redirect(PIUrl::createUrl('/home/index'));
	}
	
	public function actionComment(){
		$model = new CommentsSchedules;
		if(isset($_POST['CommentsSchedules'])){
			$model->attributes = $_POST['CommentsSchedules'];
			$model->created = time();
			$model->is_schedule = 0;

			if($model->save())
			{
				Yii::app()->user->setFlash('success', translate('Gửi thư góp ý thành công.'));
			}
		}
		$this->redirect(PIUrl::createUrl('/home/index'));
	}
	
	public function actionQuestionAnswers(){
	
		$arrQuestion = QuestionAnswers::model()->findAll();
		$this->render("questionAnswers", array("arrQuestion"=>$arrQuestion));
	}
	
	public function actionNews(){
	
		$criteriaNew = new CDbCriteria; 
		$criteriaNew->order = "id DESC";		
		$item_count = News::model()->count();
		$pages = new CPagination($item_count);
		$pages->setPageSize(5);
		$pages->applyLimit($criteriaNew);

		$arrInfo = News::model()->findAll($criteriaNew);
		
		$this->render("news", array(
			"arrInfo" => $arrInfo,
			'item_count'=>$item_count,
			'page_size'=>5,
			'items_count'=>$item_count,
			'pages'=>$pages,
		));
	}
	
	public function actionNewsDetail($alias){

		$criteria = new CDbCriteria;  
		$criteria->addCondition("alias='{$alias}'");
		$arrNew = News::model()->find($criteria);

		$this->render("newsDetail", array("arrNew"=>$arrNew));
	}
	
	public function actionSupport($id = null){
		
		switch($id){
			case 3: {
				$arrData = Informations::model()->findByPk($id);
				$this->render("supports", array("arrData"=>$arrData, 'title' => 'Hỗ trợ ngân hàng')); break;
			}
			case 4: {
				$arrData = Informations::model()->findByPk($id);
				$this->render("supports", array("arrData"=>$arrData, 'title' => 'Hỗ trợ chứng khoáng')); break;
			}
			case 5: {
				$criteria = new CDbCriteria;  
				$criteria->addCondition("type_document=4");
				$arrInfo = Documents::model()->findAll($criteria);
				$arrData = Informations::model()->findByPk($id);
				$this->render("land", array("arrInfo"=>$arrInfo,'arrData'=>$arrData, 'title' => 'Hỗ trợ đất đai')); break;
			}
			case 6: {
				$arrData = Informations::model()->findByPk($id);
				$this->render("supports", array("arrData"=>$arrData, 'title' => 'Hỗ trợ tỉ giá')); break;
			}
			default: $this->redirect(PIUrl::createUrl('/home/'));
		}
	}
	
	public function actionAlbumImage(){
	
		$criteria = new CDbCriteria;
        $total = Images::model()->count();
			
		$pages = new CPagination($total);
		$pages->pageSize = 12;
		$pages->applyLimit($criteria);

		$arrImages = Images::model()->findAll($criteria);
	
		$this->render("albumImage",array(
			'arrImages' => $arrImages,
			'pages' => $pages,));
	}
	
	public function actionError()
	{
		$this->layout = 'main';
		$this->pageTitle = "Phòng công chứng số 3 - Error";
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', array('error'=>$error));
		}
	}
}